﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Extend.Controls
{
    class MyAttached : DependencyObject
    {
        public static ICommand GetCommand(DependencyObject obj) { return (ICommand)obj.GetValue(CommandProperty); }
        public static void SetCommand(DependencyObject obj, ICommand value) { obj.SetValue(CommandProperty, value); }
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand),
            typeof(MyAttached), new PropertyMetadata(null, CommandChanged));
        private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var textbox = d as TextBox;
            if (textbox == null)
                throw new NotSupportedException(d.ToString());
            var command = e.NewValue as ICommand;
            if (command != null)
                textbox.KeyDown += (s, arg) =>
                {
                    if (arg.Key == Windows.System.VirtualKey.Enter)
                        command.Execute(null);
                };
        }
    }
}
